﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.Xml;
using Inicio.Entities;

namespace Inicio.Forms.Obras
{

    
    public partial class SearchObra : Form
    {
        List<SearchField> fields;

        private void LoadSearchConfig()
        {
            this.fields = new List<SearchField>();
            SearchField f;

            XmlDocument xDoc = new XmlDocument();
            xDoc.Load( Application.StartupPath + @"\SearchConfig.xml");

            var fields = xDoc.SelectSingleNode("SearchConfig/Obras/Fields");
            foreach (XmlNode field in fields.ChildNodes)
            {
                f = new SearchField();

                if (field.Attributes["Label"] != null)
                    f.Label = field.Attributes["Label"].InnerText;
                if (field.Attributes["Control"] != null)
                    f.Control = field.Attributes["Control"].InnerText;
                if (field.Attributes["Table"] != null)
                    f.Table = field.Attributes["Table"].InnerText;
                if (field.Attributes["FilterField"] != null)
                    f.FilterField = field.Attributes["FilterField"].InnerText;
                if (field.Attributes["DisplayField"] != null)
                    f.DisplayField = field.Attributes["DisplayField"].InnerText;
                if (field.Attributes["ValueField"] != null)
                    f.ValueField = field.Attributes["ValueField"].InnerText;

                this.fields.Add(f);
            }
        }

        public SearchObra()
        {
            InitializeComponent();
        }

        private void SearchObra_Load(object sender, EventArgs e)
        {
            LoadSearchConfig();
            NewControl();
        }

        public void NewControl()
        {
            pnlControls.Controls.Add(new ucSearchField(this, fields));
        }

        public void RemoveControl(Control ctl)
        {
            if (pnlControls.Controls.Count > 1)
                pnlControls.Controls.Remove(ctl);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            //CreateDt();
            DataTable dt = Search();
            gv.DataSource = dt;
        }

        //private void CreateDt()
        //{
        //    gv.AutoGenerateColumns = false;

        //    gv.Columns.Add(CreateColumn("Id Obra", "Column1", 50));
        //    gv.Columns.Add(CreateColumn("Nombre de Obra", "Column2", 150));
        //    gv.Columns.Add(CreateColumn("Prestador", "Column3", 150));
        //    gv.Columns.Add(CreateColumn("Tipo de Obra", "Column4", 150));
        //    gv.Columns.Add(CreateColumn("Número de Contrato", "Column4", 150));
        //    gv.Columns.Add(new DataGridViewButtonColumn());
        //}

        //private DataGridViewColumn CreateColumn(string header, string prop, int width)
        //{
        //    DataGridViewColumn res = new DataGridViewTextBoxColumn();
        //    res.HeaderText = header;
        //    res.DataPropertyName = prop;
        //    res.Width = width;

        //    return res;
        //}

        private DataTable Search()
        {
            string qry = "SELECT * FROM WORK LEFT JOIN SPOUT ON WORK.Id = SPOUT.WorkId WHERE 1=1 ";
            Core.Repositories.SearchRepository search = new Core.Repositories.SearchRepository();

            return search.SearchInDB(qry, pnlControls);
        }

    }
}
